import React from 'react';
import { Tree } from 'antd';
import globalConfig from 'config.js';
import DBTable from './TreeDbTableView';
import './index.less';
import ajax from '../../../utils/ajax';

const TreeNode = Tree.TreeNode;

//  定义右侧树形控件

var _this;

class ChildTree extends React.Component {

  state = {
    treeName: "",
    data: [],
    // requestParams:  [],
    requestParams:  { parentId: 1 }
  };

  constructor(props) {
    super(props);
    // this.initTreeName(this.props);
    _this = this;
  }

  componentWillMount() {
    this.initTreeName(this.props);
  }

  refreshTree=(id)=>{
    this.initData(id);
  }

  initTreeName(props) {
    var routes;

    if (props.routes == undefined) {
      routes = props;
    }
    else {
      routes = props.routes.pop();
    }

   this.initData();
  }

  ///module/tree/list

  initData(id) {
    var self = this;
    const url = `/module/tree/list`;  // 拼接要请求的url地址
    ajax.post({
      url: url,
      success: function (res) {
        self.data = res.data.list;
        self.initTree(id);
      }
    })
  }

  initTree(id) {
    const examples = [{
        name: "授信管理",
        id: "8",
        url: "#",
        mixId:"crediManagement",
        sn: "crediManagement",
        children: [
          {
            id:1000,
            name: "人工审核查询",
            mixId: "saa",
            children: [],
            description: "人工审核查询",
            priority: 91,
            url: "/cms/manage/CreditManagement/ManualAuditQuery"
          },
          {
            id:1001,
            name: "白名单管理",
            mixId: "saa1",
            children: [],
            description: "白名单管理",
            priority: 92,
            url: "/cms/manage/CreditManagement/WhiteListManagement"
          },
          {
            id:1002,
            name: "黑名单管理",
            mixId: "saa2",
            children: [],
            description: "黑名单管理",
            priority: 93,
            url: "/cms/manage/CreditManagement/BlackListManagement"
          }
        ]
      }, {
        name: "借据管理",
        id: "9",
        url: "#",
        sn: "IOUs",
        mixId:"mhaas",
        children: [
          {
            name: "借据信息列表",
            id:1030,
            mixId: "Iu",
            children: [],
            description: "借据信息列表",
            priority: 234,
            url: "/cms/manage/Ioumanagement/information"
          },
          {
            name: "订单信息列表",
            mixId: "Iu1",
            id:1040,
            children: [],
            description: "订单信息列表",
            priority: 94,
            url: "/cms/manage/Ioumanagement/Order"
          },
          {
            name: "放款结构查询",
            mixId: "Iu2",
            id:1056,
            children: [],
            description: "放款结构查询",
            priority: 95,
            url: "/cms/manage/Ioumanagement/Loan"
          }
        ]
      },
      {
        name: "贷后管理",
        id: "10",
        url: "#",
        sn: "postloanmanagement",
        mixId:"postloanmanagement",
        children: [
          {
            name: "我的催收列表",
            mixId: "postloanmanagement_one",
            children: [],
            id:662,
            description: "我的催收列表",
            priority: 96,
            url: "/cms/manage/PostLoanManagement/Collection"
          },
          {
            name: "逾期列表",
            mixId: "postloanmanagement_two",
            children: [],
            id:631,
            description: "逾期列表",
            priority: 97,
            url: "/cms/manage/PostLoanManagement/Overdue"
          }
        ]
      }
    ]
    const dataList = this.data.children;
    // examples.forEach((i)=>{
    //   dataList.push(i)
    // })

    // console.log(dataList)
    // 根tree的key设置
    this.firstId = null;
    this.firstId = (this.firstId !== null) ? this.firstId : this.data.id;
    let firstNode=[];

    let defauleId;

    defauleId=(id)?id:this.data.id;
    const defaultName=this.data.name;

    firstNode.push(defauleId.toString());

    const treeNode=dataList.map((node) => {
     
      return (<TreeNode key={node.id} title={node.name}>
        {node.children.map(function (item) {
          return (<TreeNode title={item.name} key={item.id} />);
        })}

      </TreeNode>);
    });

    const tree = <Tree showLine defaultSelectedKeys={firstNode} onSelect={this.onSelect}><TreeNode title={defaultName} key={this.firstId}>{treeNode}</TreeNode></Tree>;

    this.setState({ "tree": tree ,requestParams: { parentId: defauleId } });

  }

  onSelect(info) {
    if (info.length > 0) {
      let parentId = info.join("");
      // console.log(parentId);
      _this.setState({ requestParams: { parentId: parentId } });
    }
  }

  render() {
    return (
      <div className="childTreeCon">
        <aside className="childTreeAside">
          {this.state.tree}
        </aside>
        <section className="childTreeSec">
          <DBTable tableName='Module' refreshTree={this.refreshTree} requestParams={this.state.requestParams} />
        </section>
        <div className="clear"></div>
      </div>
    )
  }

}

export default ChildTree;
